import ip from "ip";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import Fastify from "fastify";
import vconf from "../utils/vconf.js";
const fastify = Fastify({ logger: true });
const config = vconf("config.yml");

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export default function () {
  //@proxy
  for (let i in config.proxy) {
    fastify.register(import("fastify-http-proxy"), config.proxy[i]);
  }

  //@public_static
  fastify.register(import("fastify-static"), {
    root: join(__dirname, "..", "public"),
    prefix: "/",
  });

  fastify.listen(3004, "0.0.0.0", (err) => {
    if (err) throw err;
    console.log(`静态服务已启动: http://${ip.address()}:3004/`);
  });
}
